﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ReportingManagement
{
    public partial class DataImportLog : Form
    {
        public enum ContainerType
        {
            /// <summary>
            /// Text box control to hold log entries
            /// </summary>
            TEXT_BOX = 0,

            /// <summary>
            /// List box control to hold log entries
            /// </summary>
            LIST_BOX = 1
        }

        private ContainerType m_Container = ContainerType.TEXT_BOX;

        public DataImportLog(ContainerType container)
        {
            InitializeComponent();
            m_Container = container;
            if (container == ContainerType.LIST_BOX)
            {
                lstLog.Visible = true;
                lblEntryCount.Visible = true;
            }
            if (container == ContainerType.TEXT_BOX)
            {
                txtLog.Visible = true;
                lblEntryCount.Visible = false;
            }
            lblEntryCount.Text = String.Empty;
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            DialogResult response = saveFileDialog.ShowDialog(this);
            if (response == System.Windows.Forms.DialogResult.OK)
            {
                StringBuilder sBuilder = new StringBuilder();
                switch (m_Container)
                {
                    case ContainerType.LIST_BOX:
                        foreach (string i in lstLog.Items)
                        {
                            sBuilder.Append(i);
                            sBuilder.Append(Environment.NewLine);
                        }
                        break;

                    case ContainerType.TEXT_BOX:
                        sBuilder.Append(txtLog.Text);
                        break;
                }

                string log = sBuilder.ToString();
                StreamWriter writer = new StreamWriter(saveFileDialog.FileName);
                writer.Write(log);
                writer.Flush();
                writer.Close();
            }
        }

        public void AddLogLine(string log)
        {
            switch (m_Container)
            {
                case ContainerType.LIST_BOX:
                    lstLog.Items.Add(log);
                    int count = lstLog.Items.Count;
                    if (count == 1) lblEntryCount.Text = String.Format("{0} entry", count);
                    else lblEntryCount.Text = String.Format("{0} entries", count);
                    break;

                case ContainerType.TEXT_BOX:
                    txtLog.AppendText(log);
                    txtLog.AppendText(Environment.NewLine);
                    break;
            }
        }

        public void AddLog(string log)
        {
            switch (m_Container)
            {
                case ContainerType.LIST_BOX:
                    lstLog.Items.Add(log);
                    int count = lstLog.Items.Count;
                    if (count == 1) lblEntryCount.Text = String.Format("{0} entry", count);
                    else lblEntryCount.Text = String.Format("{0} entries", count);
                    break;

                case ContainerType.TEXT_BOX:
                    txtLog.AppendText(log);
                    break;
            }
        }

        public void ClearLog()
        {
            if (m_Container == ContainerType.TEXT_BOX) txtLog.Clear();
            if (m_Container == ContainerType.LIST_BOX) lstLog.Items.Clear();
            lblEntryCount.Text = "No entry";
        }
    }
}
